<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/9/2
 * Time: 15:13
 */
namespace App\Services;

use App\Repositories\GalleryRepository;

class GalleryService extends CommonService
{
    protected $galleryRepository;

    public function __construct(GalleryRepository $galleryRepository)
    {
        $this->galleryRepository = $galleryRepository;
    }

    /**
     * 获取图片
     * @param $imageType
     * @return \Illuminate\Http\JsonResponse
     */
    public function getImages($imageType, $categoryId = 0, $page = 0, $limit = 100)
    {
        //海报类型
        if (2 == $imageType) {
            $categoryId = (0 == $categoryId) ? "1, 2, 3" : $categoryId;
        }
        $limit = ($limit > 0) ? $limit : 100;
        $gallery  = $this->galleryRepository->getByImageType($imageType, $categoryId, $page, $limit);
        $data = [];
        if (!empty($gallery->toArray())) {
            foreach ($gallery as $key=>$value) {
                $data[$key] = $value;
                $data[$key]['shuffling_images'] = !empty($value['shuffling_images']) ? getOssImageUrl($value['shuffling_images']) : '';
            }
        }
        return $this->responseJson(0, $data);
    }

    public function getImagesDownloadPoint($id)
    {
        $gallery = $this->galleryRepository->byId($id);
        if (empty($gallery)) {
            return $this->responseJson(10204);//图片不存在
        }
        $gallery->point += 1;
        $gallery->save();
        return $this->responseJson(0);
    }
}